
init("0", 0)

clock=false
conf = {
    ["home"] = {547, 420, 200, 200, 200},
    ["PC"] = {470, 1446, 0, 0, 0},
    ["PB"] = {520, 1446, 0, 0, 0},
    ["PA"] = {570, 1441, 0, 0, 0},
    ["P0"] = {620, 1441, 0, 0, 0},
    ["PM"] = {670, 1441, 0, 0, 0},
    ["PN"] = {720, 1441, 0, 0, 0},
    ["PP"] = {770, 1441, 0, 0, 0},
}

function is_sameColor(r1, g1, b1, r2, g2, b2)
    return math.abs(r1 - r2) < 10 and math.abs(g1 - g2) < 10 and math.abs(b1 - b2) < 10
end

function getRealCord(key)
    return conf[key][1],conf[key][2],conf[key][3],conf[key][4],conf[key][5]
end

function is_okayColorForPointWithKey(key)
    local x, y, r, g, b = getRealCord(key)
    local realR, realG, realB = getColorRGB(x, y)
    return is_sameColor(r, g, b, realR, realG, realB)
end

function touchDU(x, y)
    touchDown(1, x, y)
    mSleep(10)
    touchUp(1, x, y)
end


function is_home()
    return is_okayColorForPointWithKey("home")
end

function okayTouch()
    keepScreen(true);
    local pc, pb, pa, p0, pm, pn, pp = is_okayColorForPointWithKey("PC"), is_okayColorForPointWithKey("PB"), is_okayColorForPointWithKey("PA"), is_okayColorForPointWithKey("P0"), is_okayColorForPointWithKey("PM"),
    is_okayColorForPointWithKey("PN"), is_okayColorForPointWithKey("PP")
    keepScreen(false);

    if clock then
        return p0 == true and pm == true and pn == true and pp == true
    else
        return p0 == true and pb == true and pa == true and pc == true
    end

end

while true do
    mSleep(5)

    if is_home() then
        touchDU(624, 1150)
        mSleep(2000)
    elseif okayTouch() then
        touchDU(900, 1760)
        mSleep(100)
    end

end
